/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.framework.base.dao.DaoHelper;
import com.boxin.ims.modules.wechat.entity.ProductCategory;
import com.boxin.ims.modules.wechat.service.ProductCategoryService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.web.BaseController;

/**
 * 商品店家类别Controller
 * @author Jakemanse
 * @version 2013-08-12
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/productCategory")
public class ProductCategoryController extends BaseController {

	@Autowired
	private ProductCategoryService productCategoryService;
	
	@Autowired
	private DaoHelper daoHelper;
	
	@ModelAttribute
	public ProductCategory get(@RequestParam(required=false) Long id) {
		if (id != null){
			return productCategoryService.get(id);
		}else{
			return new ProductCategory();
		}
	}
	
	@RequiresPermissions("wechat:productCategory:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProductCategory productCategory, HttpServletRequest request, HttpServletResponse response, Model model) {
//        Page<ProductCategory> page = productCategoryService.find(new Page<ProductCategory>(request, response), productCategory); 
//        model.addAttribute("page", page);
        
        List<ProductCategory> pcList = productCategoryService.findAll();
        List<ProductCategory> list = Lists.newArrayList();
        ProductCategory.sortList(list, pcList, 1L);
        model.addAttribute("list", list);
        
        
		return "modules/wechat/productCategoryList";
	}

	@RequiresPermissions("wechat:productCategory:view")
	@RequestMapping(value = "form")
	public String form(ProductCategory productCategory, Model model) {
		
		List<ProductCategory> categoryList = productCategoryService.findRootCategory();
		if(categoryList == null || categoryList.isEmpty()){
			categoryList = Lists.newArrayList();
			ProductCategory pc = new ProductCategory();
			pc.setName("所有类别");
			pc.setValue(0);
			pc.setId(1L);
			productCategory.setParent(pc);
			categoryList.add(pc);
		}
		
		if(productCategory.getId() == null){
			//Integer maxValue = productCategoryService.getMaxValueByParentId(productCategory.getParent().getId());
			Long parentId = 1L;
			if(productCategory.getParent() != null && productCategory.getParent().getId() != null){
				parentId = productCategory.getParent().getId();
			}
			Integer maxValue = (Integer)daoHelper.queryForObject("com.boxin.ims.wechat.getMaxValueByParentId", parentId);
			maxValue = (maxValue == null) ?  0 : maxValue +1;
			
			productCategory.setValue(maxValue);
			productCategory.setSort(maxValue);
		}
		
		model.addAttribute("productCategory", productCategory);
		model.addAttribute("categoryList", categoryList);
		return "modules/wechat/productCategoryForm";
	}

	@RequiresPermissions("wechat:productCategory:edit")
	@RequestMapping(value = "save")
	public String save(ProductCategory productCategory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, productCategory)){
			return form(productCategory, model);
		}
		productCategoryService.save(productCategory);
		addMessage(redirectAttributes, "保存商品店家类别'" + productCategory.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/productCategory/?repage";
	}
	
	
	@RequestMapping(value = "child")
	public String childJson(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		List<ProductCategory> children = productCategoryService.findChildrenByParentId(id);
		response.setContentType("application/json; charset=UTF-8");
		List<Map<String, Object>> mapList = Lists.newArrayList();
		for (ProductCategory pc : children){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", pc.getId());
				map.put("value", pc.getValue());
				map.put("name", pc.getName());
				mapList.add(map);
		}
		try {
			
			PrintWriter out = response.getWriter();
			out.write(JsonMapper.getInstance().toJson(mapList));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	@RequiresPermissions("wechat:productCategory:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		productCategoryService.delete(id);
		addMessage(redirectAttributes, "删除商品店家类别成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/productCategory/?repage";
	}

}
